﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;

using Microsoft.ComplexEventProcessing;
using Microsoft.ComplexEventProcessing.ManagementService;


namespace $safeprojectname$
{
    class Program
    {
        private static ServiceHost _managementServiceHost; 

        static void Main(string[] args)
        {
            //NOTE: You can change to one of the CreateServer overloads below for different hosting models. 
            //TODO: Change the instance name to an installed instance.
            using (Server cepServer = CreateServer("Default", false, null))
            {
                //TODO: Create/connect to your application

                //TODO: Create/connect to your queries.

                Console.WriteLine("StreamInsight instance is running.");
                Console.WriteLine("Press any key to stop.");
                if (cepServer.IsEmbedded && _managementServiceHost != null)
                {
                    //Close the management service if created.
                    _managementServiceHost.Close();
                }

            }
        }

        /// <summary>
        /// Creates a StreamInsight server object using the in-process (hosted DLL) model.
        /// </summary>
        /// <param name="instanceName">Name of the StreamInsight intance</param>
        /// <param name="persistMetadata">True to persist metadata in Sql Server CE.</param>
        /// <param name="managementServiceUri">Uri of the management service. May be null.</param>
        /// <returns>The created StreamInsight server object</returns>
        /// <remarks>
        /// See http://msdn.microsoft.com/en-us/library/ff518487.aspx for information on 
        /// setting security for the WCF management service url. 
        /// </remarks>
        private static Server CreateServer(string instanceName, bool persistMetadata, Uri managementServiceUri)
        {
            Server server = null;

            if (persistMetadata)
            {
                //Create the metadata configuration. 
                //Use the assembly name for the sdf.
                SqlCeMetadataProviderConfiguration metadataProviderConfiguration =
                    new SqlCeMetadataProviderConfiguration()
                        {
                            CreateDataSourceIfMissing = true,
                            DataSource = System.Reflection.Assembly.GetEntryAssembly().GetName().Name + ".sdf"
                        };

                server = Server.Create(instanceName, metadataProviderConfiguration);
            }
            else
            {
                server = Server.Create(instanceName);
            }

            if (managementServiceUri != null)
            {
                //NOTE: You may get an exception here if you do not have the required permissions.
                try
                {
                    _managementServiceHost = new ServiceHost(server.CreateManagementService());
                    _managementServiceHost.AddServiceEndpoint(
                        typeof(IManagementService),
                        new WSHttpBinding(SecurityMode.Message), managementServiceUri);
                    _managementServiceHost.Open();
                }
                catch //(Exception ex)
                {
                    //TODO: Continue or throw exception when management service cannot be created? 
                }
            }
            return server;
        }

        /// <summary>
        /// Creates a StreamInsight server object that connects to a remote, running StreamInsight instance.
        /// </summary>
        /// <param name="instanceUri">Management service Uri for the remote instance</param>
        /// <returns>The created StreamInsight server object</returns>
        private static Server CreateServer(Uri instanceUri)
        {
            Server server = Server.Connect(new EndpointAddress(instanceUri));
            return server;
        }
    }
}
